package top.lvkun.pet.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import top.lvkun.basic.service.impl.BaseServiceImpl;
import top.lvkun.basic.utils.AjaxResult;
import top.lvkun.pet.domain.Adopt;
import top.lvkun.pet.domain.Pet;
import top.lvkun.pet.domain.PetType;
import top.lvkun.pet.mapper.AdoptMapper;
import top.lvkun.pet.mapper.PetMapper;
import top.lvkun.pet.service.IPetService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
   @Autowired
   private PetMapper petMapper;
   @Autowired
   private AdoptMapper adoptMapper;

    @Override
    public List<PetType> getPetTypes() {
        return petMapper.getPetTypes();
    }

    /**
     * 批量上架
     * @param ids
     */
    @Override
    @Transactional
    public AjaxResult patchUp(List<Long> ids) {
        //1.先通过ids批量查询
        List<Pet> list = petMapper.batchSelectByIds(ids);
        // 2.验证：如果你勾选的宠物中有已上线的宠物，则不允许上线
        List<Pet> onSalePets = list.stream().filter(pet -> pet.getState() == 0).collect(Collectors.toList());
        //断言工具类：第一个参数为true的时候，就正常通过；为false就抛出一个异常，异常消息就是第二个参数的字符串内容
        Assert.isTrue(onSalePets==null || onSalePets.size()==0, "您勾选的宠物中包含了已上架的宠物，不能重复上架！");

        //3.批量修改状态为上架状态
        petMapper.batchOnSale(ids);
        return AjaxResult.me();
    }

    /**
     * 批量下架
     * @param ids
     */
    @Override
    @Transactional
    public AjaxResult patchDown(List<Long> ids) {
        //先通过ids批量查询
        List<Pet> list = petMapper.batchSelectByIds(ids);
        //验证：如果你勾选的宠物中有已下架的宠物，则不允许下架
        List<Pet> onSalePets = list.stream().filter(pet -> pet.getState() == 1).collect(Collectors.toList());
        //断言工具类：第一个参数为true的时候，就正常通过；为false就抛出一个异常，异常消息就是第二个参数的字符串内容
        Assert.isTrue(onSalePets==null || onSalePets.size()==0, "您勾选的宠物中包含了已下架的宠物，不能重复下架！");
        //3.批量修改状态为下架状态
        petMapper.batchOffSale(ids);
        return AjaxResult.me();
    }

    @Override
    @Transactional
    public void AddOrUpdate(Pet pet) {
        if (pet.getId()==null){
            //保存宠物领养信息,并返回主键
            Adopt adopt = new Adopt();
            adopt.setName(pet.getName());
            adopt.setAddress(pet.getShop().getAddress());
            adopt.setPrice(pet.getSaleprice());
            adopt.setTitle("[本店自营宠物]");
            adopt.setResources(pet.getResources());
            adopt.setPet_type(pet.getPetType().getId());
            adopt.setShop_id(pet.getShop_id());
            adopt.setCoat_color(pet.getCoatColor().getId());
            adopt.setState(0);//默认已处理

            //宠物年龄

            //宠物性别  在pet实体类对象中添加两个属性并从前端接收
            adoptMapper.save(adopt);

            //保存宠物信息
            pet.setType_id(pet.getPetType().getId());
            pet.setAdopt_id(adopt.getId());
            petMapper.save(pet);

        }else {





            pet.setType_id(pet.getPetType().getId());
            petMapper.update(pet);
        }

    }
}
